<template>
  <j-editable-table
     ref="materialPickingDetailOutTable"
    :loading="loading"
    :columns="columns"
    :dataSource="dataSource"
    :maxHeight="300"
    :disabled="false"
    :rowNumber="true"
    :rowSelection="false"
    :actionButton="false"/>
</template>

<script>

import {FormTypes} from '@/utils/JEditableTableUtil'
import {JEditableTableModelMixin} from '@/mixins/JEditableTableModelMixin'
import {getAction, putAction} from "@/api/manage";

export default {
  name: 'MmMaterialPickingDetailOutTable',
  mixins: [JEditableTableModelMixin],
  components: {},
  data() {
    return {
      loading: false,
      dataSource: [],
      columns: [
        {
          title: '行号',
          key: 'lineNo',
          type: FormTypes.input,
          width: "70px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '物料号',
          key: 'materialNo',
          type: FormTypes.input,
          width: "120px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '物料描述',
          key: 'materialDesc',
          type: FormTypes.input,
          width: "240px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '单位',
          key: 'unit',
          type: FormTypes.select,
          dictCode:"mdm_unit,unit_name,unit_code",
          width:"100px",
          defaultValue:'',
          disabled: true
        },
        {
          title: '出库数量',
          key: 'qty',
          type: FormTypes.inputNumber,
          width: "100px",
          placeholder: '请输入${title}',
          defaultValue: '0',
          validateRules: [
            {
              handler(type, value, row, column, callback, target) {
                let {values} = target.getValuesSync({validate: false, rowIds: [row.id]});
                if (type === 'blur' || type === 'getValues') {
                  if (value < 0) {
                    callback(false, '请输入大于零的数')
                    return;
                  } else if (value > Number(values[0].stockQtyAvailable)) {
                    callback(false, '${title}不能超出可用库存量：' + values[0].stockQtyAvailable)
                    return;
                  } else if (value > Number(values[0].unpickingQty)) {
                    callback(false, '${title}不能超出未领数量：' + values[0].unpickingQty)
                    return;
                  } else {
                    callback(true)
                  }
                } else {
                  callback();
                }
              },
            },
          ],
        },
        {
          title: '未领数量',
          key: 'unpickingQty',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '可用库存量',
          key: 'stockQtyAvailable',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '库存项目',
          key: 'projectId',
          type: FormTypes.select,
          dictCode:"quote_project,project_name,id",
          width: "240px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '库房',
          key: 'warehouseId',
          type: FormTypes.sel_search,
          dictCode:"mdm_warehouse,warehouse_name,id",
          width:"140px",
          defaultValue:'',
          disabled: true
        },
        {
          title: '库位',
          key: 'storageLocationId',
          type: FormTypes.sel_search,
          dictCode:"mdm_storage_location,location_name,id",
          width:"140px",
          defaultValue:'',
          disabled: true
        },
        {
          title: '批次',
          key: 'batchNo',
          type: FormTypes.input,
          width: "140px",
          defaultValue: '',
          disabled: true,
        },
        {
          title: '制单数量',
          key: 'billQty',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '已领数量',
          key: 'pickingQty',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '库存量',
          key: 'stockQty',
          type: FormTypes.inputNumber,
          width: "100px",
          defaultValue: '0',
          disabled: true
        },
        {
          title: '含税单价',
          key: 'unitPrice',
          type: FormTypes.inputNumber,
          width: "120px",
          defaultValue: '0',
          disabled: true,
        },
        {
          title: '含税金额',
          key: 'sumPrice',
          type: FormTypes.inputNumber,
          width: "120px",
          placeholder: '请输入${title}',
          defaultValue: '0',
          disabled: true,
        },
        {
          title: '物料ID',
          key: 'materialId',
          type: FormTypes.hidden,
        },
        {
          title: '领料主表ID',
          key: 'mainId',
          type: FormTypes.hidden,
        },
        {
          title: '领料明细ID',
          key: 'detailId',
          type: FormTypes.hidden,
        },
      ],
      url: {
        handleOut: "/mm/materialPicking/handleOut",
        materialPickingStockList: "/mm/materialPicking/queryMaterialPickingStockList",
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
  },
  methods: {
    edit(record){
      let that = this
      this.loading = true;
      getAction(this.url.materialPickingStockList, {id: record.id}).then((res) => {
        if (res.success) {
          that.dataSource = res.result.records||res.result;
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    handleOk(){
      let table = this.$refs.materialPickingDetailOutTable;
      let {error, values} = table.getValuesSync({validate: true})
      if(error === 0){
        //校验通过
        this.loading = true
        let url = this.url.handleOut
        let datas = values.filter(item => Number(item.qty) > 0)
        putAction(url, {id: datas[0].mainId, materialPickingStockModelList: datas}).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      }
    },
  }
}
</script>

<style scoped>
.action-button {
  margin-bottom: 8px;
}

.gap {
  padding-left: 8px;
}
</style>